﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ISS.Models;
using System.Data;
using Utils;

namespace ISS.Business
{
    public class AlbumDAO
    {
        private ISSContext context = new ISSContext();

        public int Create(Album a)
        {
            try
            {
                a.AlbumAlias = Utils.Format.ConvertToNoSign(a.AlbumName);
                a.CreateDate = DateTime.Now;
                a.Like = 0;
                a.ViewCount = 0;
                context.dbAlbum.Add(a);
                return context.SaveChanges();
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return -1;
            }
        }

        public int Update(Album a)
        {
            try
            {
                context.Entry(a).State = EntityState.Modified;
                return context.SaveChanges();
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return -1;
            }
        }

        public int Delete(int albumId)
        {
            try
            {
                Album users = context.dbAlbum.Find(albumId);
                context.dbAlbum.Remove(users);
                return context.SaveChanges();
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return -1;
            }

        }

        public IEnumerable<Album> GetAll()
        {
            try
            {
                return context.dbAlbum.ToList();
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return null;
            }
        }

        public IEnumerable<Album> GetByUser(int userId)
        {
            try
            {
                return from album in context.dbAlbum where album.Creator == userId select album;
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return null;
            }
        }

        public IEnumerable<Album> GetByPermission(int userId, bool isFriend)
        {
            try
            {
                if (isFriend)
                {
                    return from album in context.dbAlbum where album.Permission == (byte)Album.ALBUM_PERMISSION.PROCTECTED && album.Permission == (byte)Album.ALBUM_PERMISSION.PUBLIC select album;
                }
                else
                {
                    return from album in context.dbAlbum where album.Permission == (byte)Album.ALBUM_PERMISSION.PUBLIC select album;
                }
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return null;
            }
        }

        public Album GetById(int albumId)
        {
            try
            {
                Album a = (from album in context.dbAlbum where album.AlbumId == albumId select album).First();
                a.ImageCount = (from image in context.dbImage where image.AlbumId == albumId select image).Count();
                //a.CommentCount = (from comment in context.dbAlbumComment where comment.AlbumId == albumId select comment).Count();
                return a;
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return null;
            }
        }
    }
}